"use strict";
/* Controllers */
app.controller("approverConfigListController", ['$scope', '$compile', '$location', '$rootScope', '$routeParams', 'approverConfigListService', function ($scope, $compile, $location, $rootScope, $routeParams, approverConfigListService) {

    var vm = $scope.vm;

    vm.p = {};
    vm.result = {};

    var errorFn = function (result) {
        alert(result);
    };

    $scope.list = function () {
        var vm = $scope.vm;
        var params = {};
        var procFn = function (response) {
            var data = response.data;
            if (data == null) {
                return;
            }
            vm.result.data = data;
            $('#list_table').bootstrapTable('load', data);
        };
        approverConfigListService.list(params, procFn, errorFn);
    };




    $scope.confirmDel = function (id, index) {
        vm.toDelId = id
        vm.toDelIndex = index
        $('#myModal').modal('show')
    }

    $scope.ok = function () {
        var vm = $scope.vm;
        console.log("toDelId" + vm.toDelId)
        console.log("toDelIndex" + vm.toDelIndex)
        var params = {};
        params.id = vm.toDelId
        var procFn = function (response) {
            if (response.success) {
                $('#list_table').bootstrapTable('remove', {
                    field: 'id',
                    values: [parseInt(vm.toDelId)]
                });
                alert("删除成功！")
            } else {
                alert(response.msg);
            }
        };
        approverConfigListService.delete(params, procFn, errorFn);
        $('#myModal').modal('hide')
    };

    $scope.initializeController = function () {
        var $resultTable = $('#list_table');
        $resultTable.bootstrapTable({
            pageList: [30, 50, 100, 300, 500, 1000], pageSize: 30
        });

        $resultTable.bootstrapTable('hideColumn', 'id');
        $resultTable.bootstrapTable('hideColumn', 'techOa');
        $resultTable.bootstrapTable('hideColumn', 'createTime');

        $scope.list();
    };

    $scope.initializeController();

}]);


/* Services */
app.service('approverConfigListService', ['ajaxService', function (ajaxService) {

    this.list = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/approver_config/list.do", successFunction, errorFunction);
    };

    this.delete = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/approver_config/delete.do", successFunction, errorFunction);
    };

}]);

function appRoverOpt(value, row, index) {
    var optCont2 = '<div style="width:12rem"><a type="button" class="btn btn-xs btn-primary" href="#/star/config/approver_config_edit/' + row.id + '" target="_blank">修改</a>\n';
    var optCont3 = '<a type="button" class="btn btn-xs btn-danger" onclick=angular.element(this).scope().confirmDel("' + row.id + '",' + index + ') >删除</a></div>'
    return [
        optCont2,
        optCont3
    ].join('');
}

function statusFormatter(value) {
    if(value == 1){
        return "启用";
    }
    return "禁用";
}

function actionFormatter(value) {
    if(value == 1){
        return "新建";
    }
    if(value == 2){
        return "更新";
    }
    return "删除";
}
